<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="用户账户、优惠券名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="有效状态:">
        <el-select v-model="dataForm.effectiveState" placeholder="有效状态">
          <el-option key="" label="全部" value=""></el-option>
          <el-option :key="0" label="有效" :value="0"></el-option>
          <el-option :key="1" label="失效" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="使用状态:">
        <el-select v-model="dataForm.usageState" placeholder="使用状态">
          <el-option key="" label="全部" value=""></el-option>
          <el-option :key="0" label="未用" :value="0"></el-option>
          <el-option :key="1" label="已用" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="来源:">
        <el-select v-model="dataForm.sourceType" placeholder="来源">
          <el-option key="" label="全部" value=""></el-option>
          <el-option :key="0" label="领取" :value="1"></el-option>
          <el-option :key="1" label="签到" :value="2"></el-option>
          <el-option :key="1" label="商城消费送" :value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button v-if="isAuth('usercoupon:shoppingusercoupon:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('usercoupon:shoppingusercoupon:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <!-- <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column> -->
      <!-- <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="主键">
      </el-table-column> -->
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="用户账号">
      </el-table-column>
      <el-table-column
        prop="title"
        header-align="center"
        align="center"
        label="优惠券名称">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="领取时间">
      </el-table-column>
      <!-- <el-table-column
        prop="couponId"
        header-align="center"
        align="center"
        label="优惠id">
      </el-table-column>
      <el-table-column
        prop="userId"
        header-align="center"
        align="center"
        label="用户id">
      </el-table-column>
      <el-table-column
        prop="effectiveDateStart"
        header-align="center"
        align="center"
        label="有效日期开始时间">
      </el-table-column>
      <el-table-column
        prop="effectiveDate"
        header-align="center"
        align="center"
        label="有效日期">
      </el-table-column> -->
      <!-- <el-table-column
        prop="couponType"
        header-align="center"
        align="center"
        label="优惠券类型：1商城优惠券 2联盟店优惠券">
      </el-table-column> -->
      <el-table-column
        prop="effectiveState"
        header-align="center"
        align="center"
        label="有效状态">
        <template slot-scope="scope">
          <span>{{scope.row.effectiveState === 1 ? '失效' : '有效'}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="usageState"
        header-align="center"
        align="center"
        label="使用状态">
        <template slot-scope="scope">
          <span>{{scope.row.effectiveState === 1 ? '已用' : (scope.row.effectiveState === 0 ? '未用' : '')}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="usageWays"
        header-align="center"
        align="center"
        label="使用途径">
        <template slot-scope="scope">
          <span>{{scope.row.usageWays === 1 ? '商城订单' : (scope.row.usageWays === 2 ? '联盟店订单' : (scope.row.usageWays === 3 ? '联盟店买单' : '') )}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="usageOrderNo"
        header-align="center"
        align="center"
        label="使用订单号">
      </el-table-column> -->
      <el-table-column
        prop="usageTime"
        header-align="center"
        align="center"
        label="使用时间">
      </el-table-column>
      <!-- <el-table-column
        prop="deleteStatus"
        header-align="center"
        align="center"
        label="删除状态：1已删除 0未删除">
      </el-table-column> -->
      <el-table-column
        prop="sourceType"
        header-align="center"
        align="center"
        label="来源">
        <template slot-scope="scope">
          <span>{{scope.row.usageWays === 1 ? '领取' : (scope.row.usageWays === 2 ? '签到' : (scope.row.usageWays === 3 ? '购买' : (scope.row.usageWays === 4 ? '商城消费送' : (scope.row.usageWays === 5 ? '联盟店订单消费送' : (scope.row.usageWays === 6 ? '联盟店买单消费送' : '')))) )}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="sourceId"
        header-align="center"
        align="center"
        label="
来源关联id（领取：记录 '0'）；（签到：记录 '1'）;（购买：关联联盟店购买记录支付号）;（商城订单消费送：关联的订单号）；（联盟店订单消费送：关联的订单号）；（联盟店买单消费送：关联的买单号）；">
      </el-table-column> -->
      <!-- <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <!-- <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update> -->
  </div>
</template>

<script>
  // import AddOrUpdate from './shoppingusercoupon-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          key: '',
          sourceType: '',
          usageState: '',
          effectiveState: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    // components: {
    //   AddOrUpdate
    // },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/coupon/queryUserCouponRecordList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.key,
            'effectiveState': this.dataForm.effectiveState,
            'usageState': this.dataForm.usageState,
            'sourceType': this.dataForm.sourceType,
            'usageWays': 2,
            'couponType': 2
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      // addOrUpdateHandle (id) {
      //   this.addOrUpdateVisible = true
      //   this.$nextTick(() => {
      //     this.$refs.addOrUpdate.init(id)
      //   })
      // },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/usercoupon/shoppingusercoupon/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
